package practice.range;

public class TestRange {
	public static void main(String[] args) {
		Range[] ranges = new Range[] { new Range(1, 1000000000),
				new Range(99, 1000000), new Range(10000008, 100000080),};
		System.out.println("ret:"+new TestRange().smallValue(ranges, 800));
	}

	public int smallValue(Range[] ranges, int n) {
		int smallest = Integer.MAX_VALUE;
		for (Range range : ranges) {
			if (range.down < smallest) {
				smallest = range.down;
			}
		}
		int currentValue = smallest;
		int number = 0;
		while (number < n) {
			for (Range range : ranges) {
				if (currentValue >= range.down && currentValue <= range.up) {
					number++;
				}
				if (number == n) {
					break;
				}

			}
			if (number == n) {
				break;
			}
			currentValue++;
		}
		return currentValue;

	}

	private static class Range {
		int down;
		int up;

		public Range(int down, int up) {
			super();
			this.down = down;
			this.up = up;
		}

	}
}
